/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.handlers;


import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.uml2.uml.Lifeline;

import com.cea.papyrus.diagram.sequence.sedi.edit.policy.LifelineLabelFormat;
import com.cea.papyrus.diagram.sequence.sedi.model.command.DirectEditCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.FindConnectableElementCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.SetRepresentsCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.SetSelectorCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Handles renaming of Lifelines.
 * <p>
 * This handler is used from LifelineEditPolicy.
 * </p>
 * 
 * @author frankd
 */
public class LifelineRenameHandler {

    public final EditPolicy hostPolicy;

    public LifelineRenameHandler(final EditPolicy hostPolicy) {
        this.hostPolicy = hostPolicy;
    }

    public Command getRenameCommands(final DirectEditRequest request) {
        /*
         * The code below displays a pop-up menu below the edit control. I leave it here
         * to demonstrate how to open a pop-up menu in case it should be required later.
         * -Frank final Control parent = hostPolicy.getHost().getViewer().getControl();
         * final LifelineFigure figure = (LifelineFigure)
         * ((GraphicalEditPart)hostPolicy.getHost()).getFigure(); final
         * org.eclipse.draw2d.geometry.Point gefLocation =
         * figure.getLabelBounds().getBottomLeft(); final Point location =
         * parent.toDisplay(gefLocation.x - 1, gefLocation.y + 1); final Menu popupMenu =
         * new Menu(parent); popupMenu.setLocation(location);
         * popupMenu.addMenuListener(new MenuListener() { public void menuHidden(MenuEvent
         * e) { popupMenu.dispose(); // This is possibly too early } public void
         * menuShown(MenuEvent e) { } }); final Lifeline lifeline = getLifeline(); final
         * String label = getEditorValue(request); final String propertyName =
         * LifelineLabelFormat.parseConnectableElementName(label); MenuItem
         * createPropertyMenu = new MenuItem(popupMenu, SWT.NONE);
         * createPropertyMenu.setText("Create Property '" + propertyName + "'"); // ...
         * popupMenu.setVisible(true); return null;
         */

        //
        // The logic of this method is illustrated in figure
        // "flowchart - lifeline - rename".
        //
        final Lifeline lifeline = getLifeline();
        final String label = getEditorValue(request);

        final CompoundCommand cmd = new CompoundCommand();
        cmd.setLabel("Edit Lifeline");

        // Rename the lifeline itself
        final String propertyName = LifelineLabelFormat.parseConnectableElementName(label);
        cmd.add(new DirectEditCommand(lifeline, propertyName));

        // Handle selectors
        final String selector = LifelineLabelFormat.parseSelector(label);
        cmd.add(new SetSelectorCommand(lifeline, selector));

        // Update Lifeline.Represents
        if (lifeline.getInteraction() != null) {
            final FindConnectableElementCommand findPropCmd = new FindConnectableElementCommand();
            findPropCmd.setContext(lifeline.getInteraction().getContext());
            findPropCmd.setName(propertyName);
            cmd.add(findPropCmd);

            final SetRepresentsCommand setPropCmd = new SetRepresentsCommand();
            setPropCmd.setLifeline(lifeline);

            cmd.add(new Command() {
                public void execute() {
                    setPropCmd.setRepresents(findPropCmd.getConnectableElement());
                }
            });
            cmd.add(setPropCmd);
        }

        return cmd;
    }

    @SuppressWarnings("unchecked")
    protected Lifeline getLifeline() {
        final GraphNode lifelineView = (GraphNode) hostPolicy.getHost().getModel();
        return ModelUtil.<Lifeline> getSemantic(lifelineView);
    }

    protected String getEditorValue(final DirectEditRequest request) {
        return ((String) request.getCellEditor().getValue()).trim();
    }
}
